package com.ruoyi.stuactivity.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.stuactivity.domain.StudentComprehensiveScore;
import com.ruoyi.stuactivity.service.IStudentComprehensiveScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/stuinfo/activityScore")
public class StudentComprehensiveScoreController extends BaseController {

    @Autowired
    private IStudentComprehensiveScoreService scoreService;

    /**
     * 学生报名活动接口
     */
    @PreAuthorize("@ss.hasPermi('userhuod:userhd:index')")
    @PostMapping("/signUp")
    public AjaxResult signUpActivity(@RequestBody StudentComprehensiveScore score,
                                     Authentication authentication) {
        //Long userId = SecurityUtils.getUserId();
        // 从 Authentication 获取用户名（即 JWT 中的 username）
        String studentId = authentication.getName();
        if (studentId == null || studentId.isEmpty()) {
            return AjaxResult.error("请先登录");
        }

        // 设置 studentId
        score.setStudentId(studentId);
        System.out.println("当前登录用户学号：" + studentId);

        // 调用 Service 层进行报名
        boolean result = scoreService.signUpActivity(score);
        return result ?
                AjaxResult.success("报名成功") :
                AjaxResult.error("已报名过该活动");
    }
}
